package org.dromara.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseService;
import org.dromara.demo.domain.TestDemo;
import org.dromara.demo.domain.bo.TestDemoBo;
import org.dromara.demo.domain.vo.TestDemoVo;
import org.dromara.demo.mapper.TestDemoMapper;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 测试单表Service业务层处理
 *
 * @author Lion Li
 * @date 2021-07-26
 */
@RequiredArgsConstructor
@Service
public class TestDemoServiceImpl extends BaseService {

  private final TestDemoMapper baseMapper;

  public TestDemoVo queryById(Long id) {
    return baseMapper.selectVoById(id);
  }

  public TableDataInfo<TestDemoVo> queryPageList(TestDemoBo bo, PageQuery pageQuery) {
    LambdaQueryWrapper<TestDemo> lqw = buildQueryWrapper(bo);
    Page<TestDemoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
    return TableDataInfo.build(result);
  }

  /**
   * 自定义分页查询
   */
  public TableDataInfo<TestDemoVo> customPageList(TestDemoBo bo, PageQuery pageQuery) {
    LambdaQueryWrapper<TestDemo> lqw = buildQueryWrapper(bo);
    Page<TestDemoVo> result = baseMapper.customPageList(pageQuery.build(), lqw);
    return TableDataInfo.build(result);
  }

  public List<TestDemoVo> queryList(TestDemoBo bo) {
    return baseMapper.selectVoList(buildQueryWrapper(bo));
  }

  private LambdaQueryWrapper<TestDemo> buildQueryWrapper(TestDemoBo bo) {
    Map<String, Object> params = bo.getParams();
    LambdaQueryWrapper<TestDemo> lqw = Wrappers.lambdaQuery();
    lqw.eq(bo.getDeptId() != null, TestDemo::getDeptId, bo.getDeptId());
    lqw.eq(bo.getUserId() != null, TestDemo::getUserId, bo.getUserId());
    lqw.eq(bo.getSort() != null, TestDemo::getSort, bo.getSort());
    lqw.like(StringUtils.isNotBlank(bo.getTestKey()), TestDemo::getTestKey, bo.getTestKey());
    lqw.eq(StringUtils.isNotBlank(bo.getValue()), TestDemo::getValue, bo.getValue());
    lqw.between(params.get("beginCreateTime") != null && params.get("endCreateTime") != null,
      TestDemo::getCreateTime, params.get("beginCreateTime"), params.get("endCreateTime"));
    lqw.orderByAsc(TestDemo::getId);
    return lqw;
  }

  public Boolean insertByBo(TestDemoBo bo) {
    TestDemo add = MapstructUtils.convert(bo, TestDemo.class);
    validEntityBeforeSave(add);
    boolean flag = baseMapper.insert(add) > 0;
    if (flag) {
      bo.setId(add.getId());
    }
    return flag;
  }

  public Boolean updateByBo(TestDemoBo bo) {
    TestDemo update = MapstructUtils.convert(bo, TestDemo.class);
    validEntityBeforeSave(update);
    return baseMapper.updateById(update) > 0;
  }

  /**
   * 保存前的数据校验
   *
   * @param entity 实体类数据
   */
  private void validEntityBeforeSave(TestDemo entity) {
    //TODO 做一些数据校验,如唯一约束
  }

  public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
    if (isValid) {
      //TODO 做一些业务上的校验,判断是否需要校验
    }
    return baseMapper.deleteBatchIds(ids) > 0;
  }

  public Boolean saveBatch(List<TestDemo> list) {
    return baseMapper.insertBatch(list);
  }
}
